﻿using CC.Business;
using CC.Entity;
using CCWpf.Utils; 
using System.Collections.ObjectModel; 
using System.Windows;
using System.Windows.Input;
using static CCWpf.MsgBoxWindow;

namespace CCWpf.ViewModels
{
    public class CourseSelectMainViewModel: ViewModelBase
    {
        LoginInfo loginInfo;
        RoleBLL roleBLL = new RoleBLL();
        public CourseSelectMainViewModel()
        { 
            loginInfo =new LoginInfo(); 
            if (UserId == 0)
            { 
                ShowExitBtn=Visibility.Collapsed;
                ShowLoginBtn = Visibility.Visible;
                ShowTipInfo = Visibility.Visible;
                ShowLoginInfo = Visibility.Collapsed;

            }
            InitCommands(); 
            LoadMenus();
        }

        private void InitCommands()
        {
            //命令初始化
            ShowLoginCmd = new RelayCommand(o => {
                LoginSystem();
            });
            //退出系统
            ExitCmd = new RelayCommand(o => {
                ExitSystem();
            });

            OpenPageCmd = new RelayCommand(page => {
                OpenPage(page);
            });

            ClosePageCmd = new RelayCommand(page => {
                ClosePage(page);
            });
        }
        /// <summary>
        /// 登录
        /// </summary>
        private void LoginSystem()
        {
            LoginViewModel loginVM= new LoginViewModel();
            ShowDialog("loginWin", loginVM);
            //登录页面关闭后，获取用户信息
            if (loginVM.IsLogin_Success)
            { 
                UserId = loginVM.UserId;
                //获取用户信息
                loginInfo = loginVM.LoginUser;
                LoginName = loginInfo.LoginName;
                CommonHelper.LoginName= loginInfo.LoginName;
                CommonHelper.LoginId = UserId;
                CommonHelper.LoginRoleCode = loginInfo.RoleCode;
                //RoleName = roleBLL.GetRoleName(loginInfo.RoleCode);
            }
            if (loginInfo.RoleCode == 1)
            {
                CommonHelper.IsHightAdmin = true;
            }
            ShowLoginBtn = Visibility.Hidden;
            ShowExitBtn = Visibility.Visible;
            ShowTipInfo = Visibility.Collapsed;
            ShowLoginInfo= Visibility.Visible;
            //加载导航菜单
            LoadMenus();
        }

        /// <summary>
        /// 退出
        /// </summary>
        private void ExitSystem()
        {
            CustomMsgBoxResult result = ShowQuestion("系统退出", "确定要退出系统吗？", CustomMsgBoxButton.OkCancel);
            if (result == CustomMsgBoxResult.Ok)
            {
                //修改登录者登录状态为False
                UserBLL userBLL = new UserBLL();
                bool blUpadte = true;//userBLL.ExitLogin(UserId, CommonHelper.LoginRoleCode);
                //应用程序退出处理
                if (blUpadte)
                {
                    Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
                    Application.Current.Shutdown();
                }
            }
        }
        /// <summary>
        /// 加载导航栏
        /// </summary>
        private void LoadMenus() 
        {
            MenuList = new ObservableCollection<MenuItemModel>();
            if (UserId != 9999)//未登录
            {
                //添加一个欢迎页
                MenuItemModel menuItem = new MenuItemModel()
                {
                    MenuName = "欢迎页",
                    MenuPic = "\xe603",
                    SubMenuList = new ObservableCollection<MenuItemModel>()
                    {
                        new MenuItemModel()
                        {
                            MenuName ="欢迎进入",
                            MenuURL = "Welcome.xaml",
                            OpenPageCmd = OpenPageCmd
                        },
                        //new MenuItemModel()
                        //{
                        //    IsTop = true,
                        //    MenuName ="修改密码",
                        //    MenuURL = "SM/ModifyPwdWindow.xaml", 
                        //    OpenPageCmd = OpenPageCmd
                        //},
                        new MenuItemModel()
                        { 
                            MenuName ="角色管理",
                            MenuURL = "SM/RoleListPage.xaml",
                            OpenPageCmd = OpenPageCmd
                        }
                    }
                };
                MenuList.Add(menuItem);
            }    
            else //根据登录者权限去加载
            {

            }
        }
      
        /// <summary>
        /// 打开页面
        /// </summary>
        /// <param name="win"></param>
        private void OpenPage(object win)
        {
            MenuItemModel menuItemModel = win as MenuItemModel;
            if (menuItemModel.SubMenuList.Count > 0)
            {
                menuItemModel.IsExpand = !menuItemModel.IsExpand;//设置与之前折叠状态相反
            }
            else //打开相应的页面
            {
                if (menuItemModel.IsTop)
                {
                    //顶级
                    if (menuItemModel.MenuName == "修改密码")
                    {
                        //显示窗口的方法
                        ShowDialog("modifyPwdWin", null);

                    }
                    //选课设置
                    if (menuItemModel.MenuName == "选课设置")
                    {

                    }

                }
                else
                {
                    //以TabItem 子页显示
                    if (CheckHasPage(menuItemModel.MenuName))
                    {
                        PageInfoModel page = PageList.Where(p => p.PageName == menuItemModel.MenuName).FirstOrDefault();
                        SelectedPage = page;
                        return;
                    }
                    AddTabItem(menuItemModel.MenuName,menuItemModel.MenuURL);
                }
            }
        }


        /// <summary>
        /// 关闭页面
        /// </summary>
        /// <param name="win"></param>
        private void ClosePage(object page)
        {
            PageInfoModel pageInfo = page as PageInfoModel;
            PageList.Remove(pageInfo);
            ShowTabControl();
        }
        #region 属性
        public int UserId { get; set; }

        private string loginName;
        /// <summary>
        /// 登录用户
        /// </summary>
        public string LoginName 
        {
            get {return loginName; }
            set {
                loginName = value;
                OnPropertyChanged();
            }
        }

        private string roleName;
        /// <summary>
        /// 角色名称
        /// </summary>
        public string RoleName
        {
            get { return roleName; }
            set { roleName = value; OnPropertyChanged(); }
        }

        private string loginTime;
        /// <summary>
        /// 登陆时间
        /// </summary>
        public string LoginTime
        {
            get { return loginTime; }
            set { loginTime = value; OnPropertyChanged(); }
        }

        private ObservableCollection<MenuItemModel> menuList = new ObservableCollection<MenuItemModel>();
        /// <summary>
        /// 菜单列表
        /// </summary>
        public ObservableCollection<MenuItemModel> MenuList
        {
            get { return menuList; }
            set { menuList = value;OnPropertyChanged(); }
        }

        private ObservableCollection<PageInfoModel> pageList = new ObservableCollection<PageInfoModel>();
        /// <summary>
        /// 页面列表
        /// </summary>
        public ObservableCollection<PageInfoModel> PageList
        {
            get { return pageList; }
            set { pageList = value; OnPropertyChanged(); }
        }

        private PageInfoModel selectedPage;
        /// <summary>
        /// 当前选择页
        /// </summary>
        public PageInfoModel SelectedPage
        {
            get { return selectedPage; }
            set { selectedPage = value;OnPropertyChanged(); }
        }

        private Visibility showLoginInfo=Visibility.Collapsed;
        /// <summary>
        /// 显示登录欢迎信息
        /// </summary>
        public Visibility ShowLoginInfo
        {
            get { return showLoginInfo; }
            set { showLoginInfo = value; OnPropertyChanged(); }
        }


        private Visibility showTipInfo = Visibility.Collapsed;
        /// <summary>
        /// 显示提示信息
        /// </summary>
        public Visibility ShowTipInfo
        {
            get { return showTipInfo; }
            set { showTipInfo = value; OnPropertyChanged(); }
        }

        private Visibility showLoginBtn = Visibility.Collapsed;
        /// <summary>
        /// 显示登录按钮
        /// </summary>
        public Visibility ShowLoginBtn
        {
            get { return showLoginBtn; }
            set { showLoginBtn = value; OnPropertyChanged(); }
        }
         
        private Visibility showExitBtn = Visibility.Collapsed;
        /// <summary>
        /// 显示退出按钮
        /// </summary>
        public Visibility ShowExitBtn
        {
            get { return showExitBtn; }
            set { showExitBtn = value; OnPropertyChanged(); }
        }

        private Visibility showPages = Visibility.Collapsed;
        /// <summary>
        /// 显示页集合控件
        /// </summary>
        public Visibility ShowPages
        {
            get { return showPages; }
            set { showPages = value; OnPropertyChanged(); }
        }
        #endregion

        #region 命令

        private ICommand showLoginCmd;
        /// <summary>
        /// 登录命令
        /// </summary>
        public ICommand ShowLoginCmd
        {
            get { return showLoginCmd; }
            set {
                showLoginCmd = value; OnPropertyChanged();  
            }
        }
        private ICommand exitCmd;
        /// <summary>
        /// 退出命令
        /// </summary>
        public ICommand ExitCmd
        {
            get { return exitCmd; }
            set
            {
                exitCmd = value; OnPropertyChanged();
            }
        }
        /// <summary>
        /// 打开页命令
        /// </summary>
        private ICommand OpenPageCmd;
        /// <summary>
        /// 关闭页命令
        /// </summary>
        private ICommand ClosePageCmd;
        #endregion

        private bool CheckHasPage(string pageName)
        {
            foreach (PageInfoModel page in PageList)
            {
                if (page.PageName == pageName)
                { 
                    return true;
                } 
            }
            return false;
        }

        private void AddTabItem(string pageName, string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                PageInfoModel page = new PageInfoModel();
                page.PageName = pageName;
                page.PageURL =new Uri(url,UriKind.Relative);
                page.ClosePageCmd = ClosePageCmd;
                PageList.Add(page);

                SelectedPage = page;
                //处理TabControl显示与否
            }
        }

        private void ShowTabControl()
        {
            if (PageList.Count > 0)
            {
                ShowPages = Visibility.Visible;
            }
            else
            { 
                ShowPages =Visibility.Collapsed;
            }
        }
    } 
}
